import type { ElementHiderRuleConfig } from '../../../types/constants'
import { PageHandler } from '../../../lib/page_handler'
import {
  GOOGLE_HEADER_USER_CENTER_SELECTOR,
  GooglePageType,
} from '../../../lib/page_handler/platform/google/types'
import { AppStyleCollections, ElementSelectorPosition, ElementSelectorType } from '../../../types/constants'
import { ELEMENT_IDENTIFIER_CONFIG } from '../../service/element_identifier/types'
import { addPrefixToConfig } from '../element_config'

const TAB_SELECTOR = 'div[role=\'list\'] > div:has(span:contains(\'AI 模式\'), span:contains(\'图片\'), span:contains(\'购物\'), span:contains(\'地图\'), span:contains(\'航班\'), span:contains(\'财经\')):not(:has(span:contains(\'更多\')))'

/**
 * 首页
 */
export const GOOGLE_INDEX_PAGE_ELEMENT_CONFIG: ElementHiderRuleConfig = {
  [ELEMENT_IDENTIFIER_CONFIG.default.headerNav.id]: {
    hide: false,
    selectorType: ElementSelectorType.Attribute,
    selector: 'div[role=navigation]',
    position: ElementSelectorPosition.TopCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.Index,
        GooglePageType.IndexHk,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.decoration.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: 'div:has(> a > picture)',
    nameZh: '装饰图片',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.Index,
        GooglePageType.IndexHk,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.contentAction.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: 'form[role=search] div[jscontroller] div:has(> center)',
    nameZh: '操作按钮',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.Index,
        GooglePageType.IndexHk,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.base.language.id]: {
    hide: false,
    selectorType: ElementSelectorType.Attribute,
    selector: 'div[id*=homepage_additional_languages]',
    nameZh: '可选语言',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.Index,
        GooglePageType.IndexHk,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.footer.id]: {
    hide: false,
    selectorType: ElementSelectorType.Attribute,
    selector: 'div[role=contentinfo]',
    position: ElementSelectorPosition.BottomCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.Index,
        GooglePageType.IndexHk,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
}

/**
 * 全部
 */
export const GOOGLE_ALL_PAGE_ELEMENT_CONFIG: ElementHiderRuleConfig = {
  [ELEMENT_IDENTIFIER_CONFIG.search.searchTab.id]: {
    hide: false,
    selectorType: ElementSelectorType.Multiple,
    selector: '#cnt div[data-st-cnt=\'fb\'], #cnt div[role=\'navigation\']:has(div[role=\'list\'])',
    position: ElementSelectorPosition.TopCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
        GooglePageType.Video,
        GooglePageType.ShortVideo,
        GooglePageType.Web,
        GooglePageType.News,
        GooglePageType.Books,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.search.unsupportedSearchTab.id]: {
    hide: false,
    selectorType: ElementSelectorType.JQuery,
    selector: `#cnt div[data-st-cnt='fb'] ${TAB_SELECTOR},#cnt div[role='navigation'] ${TAB_SELECTOR}`,
    position: ElementSelectorPosition.TopCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
        GooglePageType.Video,
        GooglePageType.ShortVideo,
        GooglePageType.Web,
        GooglePageType.News,
        GooglePageType.Books,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.userCenter.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: GOOGLE_HEADER_USER_CENTER_SELECTOR,
    position: ElementSelectorPosition.TopRight,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
        GooglePageType.Video,
        GooglePageType.ShortVideo,
        GooglePageType.Web,
        GooglePageType.News,
        GooglePageType.Books,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.base.metadata.id]: {
    hide: false,
    selectorType: ElementSelectorType.Multiple,
    selector: `a:has(> h3) > div, .${AppStyleCollections.Google.LINK}, .${AppStyleCollections.Google.LINK_DETAIL_BUTTON}`,
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
        GooglePageType.Web,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.component.link.id]: {
    hide: false,
    selectorType: ElementSelectorType.Multiple,
    selector: `.${AppStyleCollections.Google.LINK}, .${AppStyleCollections.Google.LINK_DETAIL_BUTTON}`,
    nameZh: '元数据-链接',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
        GooglePageType.Web,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.hrefTranslate.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: '#search #rso div:has(> a[hreftranslate])',
    nameZh: '元数据-链接-网页转换',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
        GooglePageType.Web,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.component.videoLink.id]: {
    hide: false,
    selectorType: ElementSelectorType.Multiple,
    selector: `a > h3 + .${AppStyleCollections.Google.LINK}, a:has(.${AppStyleCollections.Google.LINK}) br, .${AppStyleCollections.Google.LINK_DETAIL_BUTTON}`,
    nameZh: '元数据-链接',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.Video,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.component.bookLink.id]: {
    hide: false,
    selectorType: ElementSelectorType.Multiple,
    selector: `a > h3 + .${AppStyleCollections.Google.LINK}, a:has(.${AppStyleCollections.Google.LINK}) br`,
    nameZh: '元数据-链接',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.Books,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.component.newsLink.id]: {
    hide: false,
    selectorType: ElementSelectorType.Class,
    selector: `.${AppStyleCollections.Google.LINK_CONTAINER}`,
    nameZh: '元数据-链接',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.News,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.ai.resultAiAnswer.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: '#rcnt > div:has(#m-x-content)',
    nameZh: '结果项-AI 回答',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.featuredContent.id]: {
    hide: false,
    selectorType: ElementSelectorType.Multiple,
    selector: '#rcnt > div:not([role=\'main\']):has(div[role=\'complementary\'], > div > div > div .kp-wholepage-osrp)',
    nameZh: '结果项-精选结果',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.search.everyoneSearching.id]: {
    hide: false,
    selectorType: ElementSelectorType.Multiple,
    selector: `#botstuff #bres, #main #search #rso #kp-wp-tab-overview > div.${AppStyleCollections.Search.class.SIMILAR_SIMILAR_QUERIES}`,
    nameZh: '大家还在搜',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.search.searchPagination.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#botstuff [role=\'navigation\']',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
        GooglePageType.Web,
        GooglePageType.News,
        GooglePageType.Books,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.moreResults.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: '#rcnt > #rhs',
    nameZh: '右侧边栏-更多结果',
    position: ElementSelectorPosition.MiddleRight,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.footer.id]: {
    hide: false,
    selectorType: ElementSelectorType.Id,
    selector: '#sfooter',
    position: ElementSelectorPosition.BottomCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
        GooglePageType.Web,
        GooglePageType.News,
        GooglePageType.Books,
        GooglePageType.Video,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.base.personalization.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: '#fbar > div > div:not(.fbar)',
    nameZh: '页脚-个性化提示',
    position: ElementSelectorPosition.BottomCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
        GooglePageType.Web,
        GooglePageType.Video,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.base.position.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#fbar .fbar:has(#swml)',
    nameZh: '页脚-位置信息',
    position: ElementSelectorPosition.BottomCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
        GooglePageType.Web,
        GooglePageType.News,
        GooglePageType.Books,
        GooglePageType.Video,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.footerNavigation.id]: {
    hide: false,
    selectorType: ElementSelectorType.Id,
    selector: '#fsl',
    nameZh: '页脚-导航',
    position: ElementSelectorPosition.BottomCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.All,
        GooglePageType.Web,
        GooglePageType.News,
        GooglePageType.Books,
        GooglePageType.Video,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
}

export const GOOGLE_ADVANCED_PAGE_ELEMENT_CONFIG: ElementHiderRuleConfig = {
  [ELEMENT_IDENTIFIER_CONFIG.base.title.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '.bottom-wrapper .appbar',
    position: ElementSelectorPosition.TopLeft,
    enable: () => {
      const supportPages = [
        GooglePageType.Advanced,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.headerNav.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '.bottom-wrapper header',
    position: ElementSelectorPosition.TopCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.Advanced,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.alsoDo.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: '.bottom-wrapper form + div',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.Advanced,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.footer.id]: {
    hide: false,
    selectorType: ElementSelectorType.Id,
    selector: '#footcnt',
    position: ElementSelectorPosition.BottomCenter,
    enable: () => {
      const supportPages = [
        GooglePageType.Advanced,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GooglePageType)
    },
  },
}

/**
 * 所有配置
 */
export const GOOGLE_WEB_ELEMENT_CONFIG: ElementHiderRuleConfig = {
  ...addPrefixToConfig(GOOGLE_INDEX_PAGE_ELEMENT_CONFIG, 'index'),
  ...addPrefixToConfig(GOOGLE_ALL_PAGE_ELEMENT_CONFIG, 'all'),
  ...addPrefixToConfig(GOOGLE_ADVANCED_PAGE_ELEMENT_CONFIG, 'advanced'),
}
